package com.cn.web.menu.controller;

import com.cn.annoation.Logs;
import com.cn.menu.ButtonsDO;
import com.cn.menu.MenuDO;
import com.cn.msg.ResultList;
import com.cn.msg.ResultMsg;
import com.cn.user.UserDO;
import com.cn.util.BaseController;
import com.cn.util.JsonUtil;
import com.cn.web.menu.service.MenuService;
import com.cn.web.menu.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * description: MenuController <br>
 * date: 21.12.16 11:53 <br>
 * author: cn_yaojin <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping(value = "admin/menu")
@Api(tags = "用户中心--菜单")
public class MenuController extends BaseController {


    @Autowired
    private MenuService menuService;

    public static List empty = new ArrayList<>();

    
    @ApiOperation(value = "id查看详情")
    @GetMapping(value = "find")
    public ResultMsg<MenuVO> find(
            @ApiParam(name = "id") @RequestParam String id
    ) {
        return ResultMsg.builder().setData(this.menuService.findById(id));
    }

    
    @Logs(value = "编辑", desc = "菜单")
    @ApiOperation(value = "编辑")
    @PostMapping(value = "edit")
    public ResultMsg edit(
            @RequestBody @Validated MenuVO info
    ) {
        MenuDO menu = new MenuDO();
        BeanUtils.copyProperties(info, menu);
        if (info.getButtonArray() != null) {
            List<String> buttons = new ArrayList<>();
            for (ButtonsDO buttonsEntity : info.getButtonArray()) {
                buttons.add(buttonsEntity.getNo());
            }
            menu.setButtons(JsonUtil.toJson(buttons));
        }
        this.menuService.edit(menu);
        return ResultMsg.builder();
    }

    
    @ApiOperation(value = "菜单列表（树形列表不分页）")
    @GetMapping(value = "page")
    public ResultMsg<List<MenuVO>> page(
    ) {
        List<MenuVO> menus = this.menuService.listAllMenu();
        return ResultMsg.builder().setData(menus);
    }

    
    @Logs(value = "删除菜单", desc = "菜单")
    @ApiOperation(value = "删除菜单")
    @GetMapping(value = "del")
    public ResultMsg del(
            @ApiParam(name = "id", value = "id") @RequestParam String id
    ) {
        this.menuService.del(id);
        return ResultMsg.builder();
    }

    
    @ApiOperation(value = "系统中所有菜单(角色编辑分配权限使用该接口)")
    @GetMapping(value = "listMenus")
    public ResultList<MenuVO> listMenus() {
        List<MenuVO> menus = this.menuService.listAllMenu();
        joinMenuAndButton(menus);
        return ResultList.builder().setData(menus);
    }


    
    @ApiOperation(value = "登录人获取左侧的菜单")
    @GetMapping(value = "listMyMenus")
    public ResultList<MenuVO> listMyMenus(
    ) {
        UserDO token = super.getUser();
        String userId = null;
        if (token.getSuperFlag().equalsIgnoreCase("1")) {
            userId = token.getId().toString();
        }
        List<MenuVO> menus = this.menuService.listMenus(userId);
        if (menus == null || menus.size() == 0) {
            return ResultList.builder().setData(this.menuService.listMenuByParentId("0"));
        }
        return ResultList.builder().setData(menus);
    }

    public static void joinMenuAndButton(List<MenuVO> menus) {
        for (MenuVO menu : menus) {
            if (menu.getChild() != null && menu.getChild().size() > 0) {
                joinMenuAndButton(menu.getChild());
            } else {
                if (menu.getButtonArray() != null && menu.getButtonArray().size() > 0) {
                    List<MenuVO> tmpMenus = new ArrayList<>();
                    for (ButtonsDO buttonsEntity : menu.getButtonArray()) {
                        MenuVO tmpMenu = new MenuVO();
                        tmpMenu.setMenuNo(menu.getMenuNo() + "_" + buttonsEntity.getNo())
                                .setMenuName(buttonsEntity.getName()).setParentNo(menu.getMenuNo());
                        tmpMenu.setLevel(100).setChild(empty);
                        tmpMenus.add(tmpMenu);
                    }
                    menu.setChild(tmpMenus);
                } else {
                    menu.setChild(empty);
                }
            }
        }
    }

}
